package com.tang.server;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

import java.nio.charset.StandardCharsets;

public class MyChannelInitializer extends ChannelInitializer<SocketChannel> {

    private final int GBK = 2;
    private final int UTF8 = 3;

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        // 基于换行符号
        socketChannel.pipeline().addLast(new LineBasedFrameDecoder(1024));
        // 解码转String，注意根据自己的格式调整GBK与UTF8
        socketChannel.pipeline().addLast(new StringDecoder(StandardCharsets.UTF_8));
        // 编码转String，注意根据自己的格式调整GBK与UTF8
        socketChannel.pipeline().addLast(new StringEncoder(StandardCharsets.UTF_8));        
        // 在管道中添加我们自己的接收数据的实现方法
        socketChannel.pipeline().addLast(new MyServerHandler());
    }

}
